package com.azunitech.apm.bundles.shell.commands;

import java.io.PrintStream;

import org.osgi.framework.Bundle;
import org.osgi.framework.BundleContext;
import org.osgi.framework.BundleException;
import com.azunitech.apm.bundles.shell.BasicCommand;

public class CommandSTOP extends BasicCommand<BundleContext>
{	
	private static final String CDM_KEY = "stop";
	private static final String CMD_DESC = "stop a bundle with bundle id";
	
	public String getKey() {
		// TODO Auto-generated method stub
		return CommandSTOP.CDM_KEY;
	}

	public String getDesc() {
		// TODO Auto-generated method stub
		return CommandSTOP.CMD_DESC;
	}

	public void exec(String args, PrintStream out, PrintStream err) 
	{
		String str = args.substring(5).trim();
		BundleContext context = this.m_target;
		new SelfStopThread(context.getBundle( Long.parseLong(str))).start();
	}
	
	private static final class SelfStopThread extends Thread 
	{
	    private final Bundle m_self;

	    public SelfStopThread(Bundle self) 
	    {
	    	super("SelfStopThread Bundle " + self.getBundleId());
	    	m_self = self;
	    }

	    public void run() 
	    {
	    	try 
	    	{
	    		m_self.stop();
	    	} 
	    	catch (BundleException e) 
	    	{
	    		// Ignore
	    	}
	    }
	 }
}


